package migu.java2excel.code;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import migu.java2excel.entity.Department;

public class Java2excel {

	public void export0(List<Department> departments, String imgName, String[] titles, FileOutputStream outputStream)
			throws IOException {
		XSSFWorkbook workBook = new XSSFWorkbook();
		// CellStyle cellStyle = workBook.createCellStyle();
		// cellStyle.setFillForegroundColor((short) 13);
		// cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
		// cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		XSSFSheet sheet = workBook.createSheet("按时间统计维度导出");
		// this.fillImg(sheet, workBook, imgName);

		// ExportUtil exportUtil = new ExportUtil(workBook, sheet);
		// XSSFCellStyle headStyle = exportUtil.getHeadStyle();
		// XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();

		// 构建表头
		XSSFRow headRow = sheet.createRow(0);
		headRow.setHeight((short) (40 * 15));
		CellStyle xssfCellStyle = workBook.createCellStyle();
		xssfCellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
		xssfCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
		XSSFFont font = workBook.createFont();
		font.setFontName("黑体");
		font.setFontHeightInPoints((short) 16);// 设置字体大小
		xssfCellStyle.setFont(font);
		xssfCellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		CellStyle body = workBook.createCellStyle();
		body.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
		body.setFillForegroundColor(IndexedColors.GREEN.getIndex());
		XSSFFont bodyFont = workBook.createFont();
		bodyFont.setFontName("宋体");
		font.setFontHeightInPoints((short) 10);// 设置字体大小
		body.setFont(bodyFont);
		body.setAlignment(XSSFCellStyle.ALIGN_CENTER);

		XSSFCell cell = null;
		for (int i = 0; i < titles.length; i++) {
			cell = headRow.createCell(i);
			// cell.setCellStyle(headStyle);
			cell.setCellValue(titles[i]);
			cell.setCellStyle(xssfCellStyle);
		}
		cell = headRow.createCell(3);
		// cell.setCellStyle(headStyle);
		cell.setCellStyle(xssfCellStyle);

		int i = 1;
		// 构建表体数据
		for (Department department : departments) {
			XSSFRow bodyRow = sheet.createRow(i);
			cell = bodyRow.createCell(0);
			// cell.setCellStyle(bodyStyle);
			cell.setCellValue(department.getName());
			cell.setCellStyle(body);

			cell = bodyRow.createCell(1);
			cell.setCellValue(department.getAddress());
			cell.setCellStyle(body);

			cell = bodyRow.createCell(2);
			cell.setCellValue(department.getBuildYear());
			cell.setCellStyle(body);

			cell = bodyRow.createCell(3);
			cell.setCellValue(department.getIsListed());
			cell.setCellStyle(body);
			i++;
		}
		for (int j = 1; j < 11; j++) {
			// column width is set in units of 1/256th of a character width
			sheet.setColumnWidth(j, 256 * 15);
		}
		try {
			workBook.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Department> departments = new ArrayList<Department>();
		boolean flag = false;
		for (int i = 1; i < 11; i++) {
			if (i > 5) {
				flag = true;
			}
			departments.add(new Department("dpt" + i, "address" + i, i, flag));
		}
		try {
			FileOutputStream outputStream = new FileOutputStream(
					"C:\\Users\\Administrator\\Desktop\\spms\\testExcel\\test.xlsx");
			String imgName = "imgName";
			List<String> list = new ArrayList<String>();
			list.add("title1");
			list.add("title2");
			list.add("title3");
			String[] titles = new String[list.size()];
			list.toArray(titles);
			Java2excel java2excel = new Java2excel();
			java2excel.export0(departments, imgName, titles, outputStream);
			outputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
